package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskIntervalCycleDAO;

import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskIntervalCycle;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for TaskIntervalCycle entities
 * 
 */

@Service("TaskIntervalCycleService")

@Transactional
public class TaskIntervalCycleServiceImpl implements TaskIntervalCycleService {

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskIntervalCycle entities
	 * 
	 */
	@Autowired
	private TaskIntervalCycleDAO taskIntervalCycleDAO;

	/**
	 * Instantiates a new TaskIntervalCycleServiceImpl.
	 *
	 */
	public TaskIntervalCycleServiceImpl() {
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public TaskIntervalCycle deleteTaskIntervalCycleTask(Integer taskintervalcycle_id, Integer related_task_id) {
		TaskIntervalCycle taskintervalcycle = taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(taskintervalcycle_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		taskintervalcycle.setTask(null);
		related_task.getTaskIntervalCycles().remove(taskintervalcycle);
		taskintervalcycle = taskIntervalCycleDAO.store(taskintervalcycle);
		taskIntervalCycleDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return taskintervalcycle;
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public TaskIntervalCycle saveTaskIntervalCycleTask(Integer id, Task related_task) {
		TaskIntervalCycle taskintervalcycle = taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		taskintervalcycle.setTask(related_task);
		related_task.getTaskIntervalCycles().add(taskintervalcycle);
		taskintervalcycle = taskIntervalCycleDAO.store(taskintervalcycle);
		taskIntervalCycleDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return taskintervalcycle;
	}

	/**
	 * Return a count of all TaskIntervalCycle entity
	 * 
	 */
	@Transactional
	public Integer countTaskIntervalCycles() {
		return ((Long) taskIntervalCycleDAO.createQuerySingleResult("select count(o) from TaskIntervalCycle o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing TaskIntervalCycle entity
	 * 
	 */
	@Transactional
	public void saveTaskIntervalCycle(TaskIntervalCycle taskintervalcycle) {
		TaskIntervalCycle existingTaskIntervalCycle = taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(taskintervalcycle.getId());

		if (existingTaskIntervalCycle != null) {
			if (existingTaskIntervalCycle != taskintervalcycle) {
				existingTaskIntervalCycle.setId(taskintervalcycle.getId());
				existingTaskIntervalCycle.setFirstExecuteDate(taskintervalcycle.getFirstExecuteDate());
				existingTaskIntervalCycle.setCycleDays(taskintervalcycle.getCycleDays());
				existingTaskIntervalCycle.setMemo(taskintervalcycle.getMemo());
			}
			taskintervalcycle = taskIntervalCycleDAO.store(existingTaskIntervalCycle);
		} else {
			taskintervalcycle = taskIntervalCycleDAO.store(taskintervalcycle);
		}
		taskIntervalCycleDAO.flush();
	}

	/**
	 * Load an existing TaskIntervalCycle entity
	 * 
	 */
	@Transactional
	public Set<TaskIntervalCycle> loadTaskIntervalCycles() {
		return taskIntervalCycleDAO.findAllTaskIntervalCycles();
	}

	/**
	 * Delete an existing TaskIntervalCycle entity
	 * 
	 */
	@Transactional
	public void deleteTaskIntervalCycle(TaskIntervalCycle taskintervalcycle) {
		taskIntervalCycleDAO.remove(taskintervalcycle);
		taskIntervalCycleDAO.flush();
	}

	/**
	 * Return all TaskIntervalCycle entity
	 * 
	 */
	@Transactional
	public List<TaskIntervalCycle> findAllTaskIntervalCycles(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<TaskIntervalCycle>(taskIntervalCycleDAO.findAllTaskIntervalCycles(startResult, maxRows));
	}

	/**
	 */
	@Transactional
	public TaskIntervalCycle findTaskIntervalCycleByPrimaryKey(Integer id) {
		return taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(id);
	}
}
